package com.zhudoo.zhilian.dao.impl;

import com.zhudoo.zhilian.dao.entity.User;
import com.zhudoo.zhilian.dao.entity.UserForm;
import com.zhudoo.zhilian.dao.entity.UserRange;
import com.zhudoo.zhilian.dao.entity.UserReport;
import com.zhudoo.zhilian.dao.repository.UserFormRepository;
import com.zhudoo.zhilian.dao.repository.UserRangeRepository;
import com.zhudoo.zhilian.dao.repository.UserReportRepository;
import com.zhudoo.zhilian.dao.repository.UserRepository;
import com.zhudoo.zhilian.util.web.BusinessException;
import com.zhudoo.zhilian.util.web.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * @author zhudao
 */
@Component
public class UserDao {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserFormRepository userFormRepository;
    @Autowired
    private UserReportRepository userReportRepository;
    @Autowired
    private UserRangeRepository userRangeRepository;

    public User findUserById(Integer id) {
        return userRepository.getOne(id);
    }

    public User findUserByToken(String token) {
        User user = userRepository.findByToken(token);
        if (user == null) {
            throw BusinessException.getInstance(ResponseUtil.INVALID, "账号不存在");
        }
        if (!user.getValid()) {
            throw BusinessException.getInstance(ResponseUtil.INVALID, "账号已被禁止");
        }
        return user;
    }

    public User findUserByPhone(String phone) {
        return userRepository.findByPhone(phone);
    }

    public User findUserByOpenId(String openId) {
        return userRepository.findByOpenId(openId);
    }

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public void saveUserForm(UserForm userForm) {
        userFormRepository.save(userForm);
    }

    public void saveUserReport(UserReport userReport) {
        userReportRepository.save(userReport);
    }

    public void saveUserRange(UserRange userRange) {
        userRangeRepository.save(userRange);
    }

    public void deleteUserRangeById(Integer id) {
        userRangeRepository.deleteById(id);
    }

    public List<UserRange> getUserRangeByUserId(int id) {
        return userRangeRepository.findAllByUserId(id);
    }

    public UserRange findUserRangeByUserIdAndOneIdAndTwoId(Integer id, Integer oneId, Integer twoId) {
        return userRangeRepository.findByUserIdAndOneIdAndTwoId(id, oneId, twoId);
    }

    public UserForm findFrom(Integer userId) {
        Pageable pageable = PageRequest.of(0, 1, Sort.by("formExpiredTime"));
        List<UserForm> userForms = userFormRepository.findByUserIdAndFormExpiredTimeGreaterThanAndValid(userId, new Date(), true, pageable);
        if (userForms.size() > 0) {
            return userForms.get(0);
        }
        return null;
    }
}
